<?php

class CompanyFollowers extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $company_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'company_id' => 'company_id', 
            'user_id' => 'user_id', 
            'date_created' => 'date_created'
        );
    }

    public function beforeValidationOnCreate()
    {
        $this->date_created = CURR_DATE;
    }

    public function insertFollowers($company_id,$user_id)
    {
        $x = new CompanyFollowers();
        $x->company_id = $company_id;
        $x->user_id = $user_id;
        return $x->save();
    }
   
    public function deleteFollowers($company_id,$user_id)
    {
       $x= CompanyFollowers::findFirst(array(
            "conditions"=>"company_id = ?0 and user_id = ?1",
            "bind"=>array(0=>$company_id,1=>$user_id)
        )); 
       return $x->delete();
    }
    public function checkIfFollowed($user_id,$company_id)
    {
        $result = CompanyFollowers::findFirst(array(
                      "conditions"=>"company_id = ?0 AND user_id = ?1",
                      "bind"=>array(0=>$company_id,1=>$user_id)
                  ));
        return $result;
    }
    public function listFollowers($company_id)
    {
        return CompanyFollowers::find(array(
                    "conditions"=>"company_id = ?0",
                    "bind"=>array(0=>$company_id)
                ));
    }
    public function listTotalFollowers($company_id)
    {
        $phql = "SELECT COUNT(*) as total FROM CompanyFollowers cf WHERE cf.company_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$company_id));
        return $data[0]->total;
    }
}
